<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="beans.Drivers" %>
<%@ page import="service.DriversService" %>
<%
    // 检查session是否存在
    if(session.getAttribute("username") == null) {
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return;
    }
    String username = (String)session.getAttribute("username");
    String role = (String)session.getAttribute("role");
    Drivers driver = (Drivers)session.getAttribute("user");
    
    // 如果需要刷新司机信息，从数据库重新获取
    String refresh = request.getParameter("refresh");
    if ("true".equals(refresh)) {
        DriversService driversService = new DriversService();
        Drivers refreshedDriver = driversService.getDriverById(driver.getDriverId());
        if (refreshedDriver != null) {
            driver = refreshedDriver;
            session.setAttribute("user", driver);
        }
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>公司用车管理系统（司机）- 个人中心</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
            color: #333;
        }
        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }
        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
        }
        .user-info {
            margin-right: 20px;
            color: #fff;
        }
        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
        }
        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
            padding: 8px 15px;
            border-radius: 4px;
            background: rgba(255,255,255,0.1);
            transition: background 0.3s ease;
        }
        .logout:hover {
            background: rgba(255,255,255,0.2);
        }
        .container {
            display: flex;
            height: calc(100vh - 56px);
        }
        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24,58,138,0.05);
        }
        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: all 0.3s;
            text-decoration: none;
            display: flex;
            align-items: center;
        }
        .menu-item i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .menu-item.active, .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24,58,138,0.10);
        }
        .main-content {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
        }
        .profile-card {
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 30px;
            margin-bottom: 20px;
        }
        .profile-header {
            display: flex;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .profile-avatar {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: #183a8a;
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3rem;
            margin-right: 30px;
        }
        .profile-info {
            flex: 1;
        }
        .profile-name {
            font-size: 1.8rem;
            font-weight: bold;
            color: #183a8a;
            margin-bottom: 5px;
        }
        .profile-role {
            color: #666;
            font-size: 1.1rem;
        }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
            margin-top: 20px;
        }
        .info-item {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            transition: all 0.3s;
        }
        .info-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .info-label {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 5px;
        }
        .info-value {
            color: #333;
            font-size: 1.1rem;
            font-weight: 500;
        }
        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s;
        }
        .btn-primary {
            background: #1da1e7;
            color: white;
        }
        .btn-primary:hover {
            background: #2176b8;
        }
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .alert {
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        @media (max-width: 900px) {
            .sidebar {
                width: 120px;
            }
            .menu-item {
                padding-left: 15px;
                font-size: 1rem;
            }
            .info-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div>欢迎登录公司用车管理系统（司机）</div>
        <div class="header-right">
            <div class="user-icon">👤</div>
            <div class="user-info">工号：<%=username%></div>
            <div class="bell-icon">🔔</div>
            <a href="<%=request.getContextPath()%>/login.jsp" class="logout">退出登录</a>
        </div>
    </div>

    <div class="container">
        <div class="sidebar">
            <a class="menu-item" href="DriversServlet?action=1234">
                <i class="fas fa-home"></i> 系统首页
            </a>
            <a class="menu-item active" href="DriversServlet?action=a">
                <i class="fas fa-user"></i> 个人中心
            </a>
            <a class="menu-item" href="DriversServlet?action=c">
                <i class="fas fa-list"></i> 全部订单
            </a>
            <a class="menu-item" href="DriversServlet?action=d">
                <i class="fas fa-tools"></i> 维修单提交
            </a>
            <a class="menu-item" href="DriversServlet?action=e">
                <i class="fas fa-money-bill-wave"></i> 用车费用提交
            </a>
        </div>

        <div class="main-content">
            <% if(request.getAttribute("error") != null) { %>
                <div class="alert alert-danger"><%=request.getAttribute("error")%></div>
            <% } %>
            <% if(request.getAttribute("message") != null) { %>
                <div class="alert alert-success"><%=request.getAttribute("message")%></div>
            <% } %>

            <div class="profile-card">
                <div class="profile-header">
                    <div class="profile-avatar">
                        <%=driver.getDriverName().charAt(0)%>
                    </div>
                    <div class="profile-info">
                        <div class="profile-name"><%=driver.getDriverName()%></div>
                        <div class="profile-role">司机</div>
                    </div>
                </div>

                <div class="info-grid">
                    <div class="info-item">
                        <div class="info-label">工号</div>
                        <div class="info-value"><%=driver.getDriverId()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">姓名</div>
                        <div class="info-value"><%=driver.getDriverName()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">性别</div>
                        <div class="info-value"><%=driver.getGender()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">联系电话</div>
                        <div class="info-value"><%=driver.getPhoneNumber()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">邮箱</div>
                        <div class="info-value"><%=driver.getEmail()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">驾驶证号</div>
                        <div class="info-value"><%=driver.getDriverLicenseNumber()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">驾驶证到期日</div>
                        <div class="info-value"><%=driver.getDriverLicenseExpirationDate()%></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">状态</div>
                        <div class="info-value"><%=driver.getStatus()%></div>
                    </div>
                </div>

                <div class="action-buttons">
                    <button class="btn btn-primary" onclick="showChangePasswordModal()">修改密码</button>
                    <button class="btn btn-secondary" onclick="refreshProfile()">刷新信息</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 修改密码模态框 -->
    <div id="changePasswordModal" class="modal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5);">
        <div class="modal-content" style="background: white; width: 400px; margin: 100px auto; padding: 20px; border-radius: 8px;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                <h2 style="margin: 0;">修改密码</h2>
                <span onclick="closeChangePasswordModal()" style="cursor: pointer; font-size: 24px;">&times;</span>
            </div>
            <form action="${pageContext.request.contextPath}/DriversServlet" method="post" class="form">
                <input type="hidden" name="action" value="changePassword">
                <input type="hidden" name="driverId" value="<%=driver.getDriverId()%>">
                
                <div style="margin-bottom: 15px;">
                    <label for="oldPassword" style="display: block; margin-bottom: 5px;">原密码</label>
                    <input type="password" id="oldPassword" name="oldPassword" required 
                           style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                </div>

                <div style="margin-bottom: 15px;">
                    <label for="newPassword" style="display: block; margin-bottom: 5px;">新密码</label>
                    <input type="password" id="newPassword" name="newPassword" required 
                           style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                </div>

                <div style="margin-bottom: 20px;">
                    <label for="confirmPassword" style="display: block; margin-bottom: 5px;">确认新密码</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" required 
                           style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                </div>

                <div style="display: flex; gap: 10px;">
                    <button type="submit" class="btn btn-primary" style="flex: 1;">确认修改</button>
                    <button type="button" class="btn btn-secondary" onclick="closeChangePasswordModal()" style="flex: 1;">取消</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        function showChangePasswordModal() {
            document.getElementById('changePasswordModal').style.display = 'block';
        }

        function closeChangePasswordModal() {
            document.getElementById('changePasswordModal').style.display = 'none';
        }

        function refreshProfile() {
            window.location.href = 'DriversServlet?action=a&refresh=true';
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            if (event.target == document.getElementById('changePasswordModal')) {
                closeChangePasswordModal();
            }
        }

        // 表单提交前验证密码
        document.querySelector('form[action*="changePassword"]').onsubmit = function(e) {
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            
            if (newPassword !== confirmPassword) {
                alert('两次输入的密码不一致');
                e.preventDefault();
                return false;
            }
            return true;
        };
    </script>
</body>
</html>